package com.example.restful.controller;

import com.example.restful.common.CommonResponse;
import com.example.restful.common.LinkDTO;
import com.example.restful.common.LinkInfo;
import com.example.restful.domain.Material;
import com.example.restful.domain.Personnel;
import com.example.restful.service.MaterialService;
import com.example.restful.utils.UrlUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 云归处清风来
 */
@RestController
@RequestMapping("materials")
@Tag(name = "实验室物资")
public class MaterialController {

    @Autowired
    MaterialService materialService;

    @GetMapping
    @Operation(summary = "获取所有实验物资的信息")
    public CommonResponse<Object> getAll() {
        List<Material> list = materialService.selectAll();
        List<LinkDTO<Material>> res = new ArrayList<>();
        if (list.isEmpty()) {
            return CommonResponse.createForSuccess("无物资记录");
        }
        for (Material material : list) {
            Map<String,String> urls = new HashMap<>();
            urls.put(LinkInfo.materialInfo,UrlUtil.getUrlWithArg(MaterialController.class, "getById", material.getId()));
            urls.put(LinkInfo.materialBorrow,UrlUtil.getUrlWithArg(BorrowController.class, "getByMaterialId", material.getId()));
            LinkDTO<Material> linkDTO = new LinkDTO<>(material, urls);
            res.add(linkDTO);
        }
        return CommonResponse.createForSuccess("查询物资成功", res);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取某个指定物资的信息")
    public CommonResponse<Object> getById(@PathVariable int id) {
        Material material = materialService.selectById(id);
        if (material == null) {
            return CommonResponse.createForSuccess("未找到相关物资");
        }
        Map<String,String> urls = new HashMap<>();
        urls.put(LinkInfo.allMaterial,UrlUtil.getUrl(MaterialController.class, "getAll"));
        urls.put(LinkInfo.materialBorrow,UrlUtil.getUrlWithArg(BorrowController.class, "getByMaterialId", material.getId()));
        LinkDTO<Material> res = new LinkDTO<>(material, urls);
        return CommonResponse.createForSuccess("查询物资成功", res);
    }

    @PostMapping()
    @Operation(summary = "添加一个物资")
    public CommonResponse<Object> addMaterial(@RequestBody Material material) {

        if (materialService.addItem(material)) {
            return CommonResponse.createForSuccess("添加物资成功", material);
        }
        return CommonResponse.createForError("添加物资失败");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改某个指定的物资的信息")
    public CommonResponse<Object> update(@PathVariable int id, @RequestBody Material material) {
        if (materialService.updateById(id, material)) {
            Material newMaterial = materialService.selectById(id);

            return CommonResponse.createForSuccess("修改物资成功", newMaterial);
        }
        return CommonResponse.createForError("修改物资失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除某个指定物资")
    public CommonResponse<Object> delete(@PathVariable int id) {
        if (materialService.deleteById(id)) {
            return CommonResponse.createForSuccess("删除物资成功");
        }
        String failedMsg;
        Material material = materialService.selectById(id);
        if (material == null) {
            failedMsg = "物资不存在,删除失败";
        } else {
            failedMsg = "物资还未被归还,删除失败";
        }
        return CommonResponse.createForError(failedMsg);
    }
}
